﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace DAO
{
    public class CompanyDAO
    {
        public static DataTable LayTatCaKHDoanhNghiep()
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }


                rs = SqlHelper.ExecuteDataset(connect, CommandType.StoredProcedure, "usp_GetAllCompany").Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }

            return rs;
        }

        public static DataTable LayKHDoanhNghiepTheoNhanVien(Int32 MaNV)
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@manv", MaNV);

                rs = SqlHelper.ExecuteDataset(connect, CommandType.StoredProcedure, "usp_GetAllCompanyByID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }

            return rs;
        }

        public static int ThemKHDoanhNghiep(Entity.Company kh)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[24];
                param[0] = new SqlParameter("@makhdoanhnghiep", kh.Ma);
                param[1] = new SqlParameter("@tendoanhnghiep", kh.Ten);
                param[2] = new SqlParameter("@diachi", kh.DiaChi);
                param[3] = new SqlParameter("@dienthoai", kh.DienThoai);
                param[4] = new SqlParameter("@fax", kh.Fax);
                param[5] = new SqlParameter("@masothue", kh.MaSoThue);
                param[6] = new SqlParameter("@quyetdinhthanhlap", kh.QuyetDinhThanhLap);
                param[7] = new SqlParameter("@ngayquyetdinh", kh.NgayQuyetDinh);
                param[8] = new SqlParameter("@coquancap", kh.CoQuanCap);
                param[9] = new SqlParameter("@coquanqlytructiep", kh.CoQuanQLyTrucTiep);
                param[10] = new SqlParameter("@loaihinh", kh.LoaiHinh);
                param[11] = new SqlParameter("@nganhkinhdoanh", kh.NganhKinhDoanh);
                param[12] = new SqlParameter("@giayphepkinhdoanh", kh.GiayPhepKinhDoanh);
                param[13] = new SqlParameter("@ngaycapgiayphep", kh.NgayCapGiayPhep);
                param[14] = new SqlParameter("@nganhkinhte", kh.NganhKinhTe);
                param[15] = new SqlParameter("@tenchutichhdqt", kh.ChuTichHDQT);
                param[16] = new SqlParameter("@namsinhchutichhdqt", kh.NamSinhChuTichHDQT);
                param[17] = new SqlParameter("@tentonggiamdoc", kh.TongGiamDoc);
                param[18] = new SqlParameter("@namsinhtgd", kh.NamSinhTongGiamDoc);
                param[19] = new SqlParameter("@tongsolaodong", kh.TongLaoDong);
                param[20] = new SqlParameter("@vondieule", kh.VonDieuLe);
                param[21] = new SqlParameter("@vonkinhdoanh", kh.VonKinhDoanh);
                param[22] = new SqlParameter("@hanmuc", kh.HanMuc);
                param[23] = new SqlParameter("@manhanvien", kh.MaNhanVien);

                return SqlHelper.ExecuteNonQuery(connect, CommandType.StoredProcedure, "usp_ThemKHDoanhNghiep", param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static int CapNhatKHDoanhNghiep(Entity.Company kh)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[24];
                param[0] = new SqlParameter("@makhdoanhnghiep", kh.Ma);
                param[1] = new SqlParameter("@tendoanhnghiep", kh.Ten);
                param[2] = new SqlParameter("@diachi", kh.DiaChi);
                param[3] = new SqlParameter("@dienthoai", kh.DienThoai);
                param[4] = new SqlParameter("@fax", kh.Fax);
                param[5] = new SqlParameter("@masothue", kh.MaSoThue);
                param[6] = new SqlParameter("@quyetdinhthanhlap", kh.QuyetDinhThanhLap);
                param[7] = new SqlParameter("@ngayquyetdinh", kh.NgayQuyetDinh);
                param[8] = new SqlParameter("@coquancap", kh.CoQuanCap);
                param[9] = new SqlParameter("@coquanqlytructiep", kh.CoQuanQLyTrucTiep);
                param[10] = new SqlParameter("@loaihinh", kh.LoaiHinh);
                param[11] = new SqlParameter("@nganhkinhdoanh", kh.NganhKinhDoanh);
                param[12] = new SqlParameter("@giayphepkinhdoanh", kh.GiayPhepKinhDoanh);
                param[13] = new SqlParameter("@ngaycapgiayphep", kh.NgayCapGiayPhep);
                param[14] = new SqlParameter("@nganhkinhte", kh.NganhKinhTe);
                param[15] = new SqlParameter("@tenchutichhdqt", kh.ChuTichHDQT);
                param[16] = new SqlParameter("@namsinhchutichhdqt", kh.NamSinhChuTichHDQT);
                param[17] = new SqlParameter("@tentonggiamdoc", kh.TongGiamDoc);
                param[18] = new SqlParameter("@namsinhtgd", kh.NamSinhTongGiamDoc);
                param[19] = new SqlParameter("@tongsolaodong", kh.TongLaoDong);
                param[20] = new SqlParameter("@vondieule", kh.VonDieuLe);
                param[21] = new SqlParameter("@vonkinhdoanh", kh.VonKinhDoanh);
                param[22] = new SqlParameter("@hanmuc", kh.HanMuc);
                param[23] = new SqlParameter("@manhanvien", kh.MaNhanVien);

                return SqlHelper.ExecuteNonQuery(connect, CommandType.StoredProcedure, "usp_CapNhatKHDoanhNghiep", param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static int XoaKHDoanhNghiep(String kh)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@makhdoanhnghiep", kh);
                
                return SqlHelper.ExecuteNonQuery(connect, CommandType.StoredProcedure, "usp_XoaKHDoanhNghiep", param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }
    }
}
